<?php

class CmsCateModel extends Model {
	var $tableName = 'cms_category';
	
	//生成分类Tree
	function makeTree($pid, $leven=''){
		if($leven && $pid!=0){
			$num = explode(',',$pid);
			if($leven<=count($num)){
				return '';
			}
		}
				
		$c = $this->where("pid='$pid'")->findAll();
		
		if( !empty($c) ){
			foreach($c as $v){
				$cTree[t]	=	$v['title'];
				$cTree[a]	=	$v['id'];
				$cTree[d]	=	$this->makeTree($v['id'], $leven);
				$cTrees[]	=	$cTree;
			}
		}
		
		return $cTrees;
	}
	//解析分类
	function digCate($array) {
		foreach($array as $k=>$v){
			$nk	=	str_replace('category','',$k);
			if(is_numeric($nk) && !empty($v)){
				$cates[$nk]	=	intval($v);
			}
		}
		$pid	=	is_array($cates)?end($cates):0;
		unset($cates);
		return intval($pid);
	}
	//解析分类树
	function digCateTree($array) {
		foreach($array as $k=>$v){
			$nk	=	str_replace('category','',$k);
			if(is_numeric($nk) && !empty($v)){
				$cates[$nk]	=	intval($v);
			}
		}
		if(is_array($cates)){
			return implode(',',$cates);
		}else{
			return intval($cates);
		}
	}
	
	function makeLiTree($pid) {
		
		if( $c = $this->where("pid='$pid'")->findAll() ){
			
			$list	.=	'<ul>';
			foreach($c as $p){
				@extract($p);
				
				$ptitle	=	"<span id='category_".$id."' title='".$title."'><a href='javascript:void(0)' onclick=\"edit('".$id."')\">".$title."</a></span>";
				$title	=	'['.$id.'] '.$ptitle;

				$list	.=	'
					<li id="li_'.$id.'">
					<span style="float:right; margin-right:10px;">
						<a href="javascript:void(0)" onclick="edit('.$id.');return false;">修改</a>';
				if(!in_array($id,array(6,59))){
					$list	.= '&nbsp;<a href="javascript:void(0)" onclick="del(\''.$id.'\');return false;">删除</a>';
				}		
				$list	.= '</span> '.$title.'
					</li>
					<hr style="height:1px;color:#ccc" />';
				
				$list	.=	$this->makeLiTree($id);
			
			}
			$list	.=	'</ul>';
			
		}
		return $list;
	}		
}

?>